﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;

namespace com.huigo.Admin.Web.product
{
    public partial class AddProduct : BasePage
    {
        private bool isModify
        {
            get
            {
                return pid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }

        private long pid
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["pid"]))
                {
                    return long.MinValue;
                }
                else
                {
                    long p = long.MinValue;
                    if (long.TryParse(Request.QueryString["pid"], out p))
                        flgModify.Text = "1";
                    return p;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
                if (isModify)
                {
                    LoadData();
                }
            }
        }

        private void BindList()
        {
            //保险公司列表
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_COMPANY_DROPLIST, argList);

            List<DropList> cDropList = Tools.ParserToDropList(parser, "CompanyId", "CompanyShortName");
            if (cDropList != null)
            {
                companyList.DataSource = cDropList;
                companyList.DataValueField = "Id";
                companyList.DataTextField = "Value";
                companyList.DataBind();
            }

            //保险种类列表
           // parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_TYPE_DROPLIST, argList);

            List<DropList> tDropList = Tools.DictToDropList(Dict.DictInsType);
            if (tDropList != null)
            {
                typeList.DataSource = tDropList;
                typeList.DataValueField = "Id";
                typeList.DataTextField = "Value";
                typeList.DataBind();
            }

            //供应商列表
            parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_SUP_DROPLIST, argList);

            List<DropList> sDropList = Tools.ParserToDropList(parser, "SupId", "SupName");
            if (sDropList != null)
            {
                supList.DataSource = sDropList;
                supList.DataValueField = "Id";
                supList.DataTextField = "Value";
                supList.DataBind();
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!VerifyField(FieldType.STRING, typeList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择产品种类！");
                return;
            }
            if (!VerifyField(FieldType.STRING, companyList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择保险公司！");
                return;
            }
            if (!VerifyField(FieldType.STRING, supList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择供应商！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proName.Text, true))
            {
                MessageBox.Show(this.Page, "产品名称不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proCode.Text, true))
            {
                MessageBox.Show(this.Page, "产品代码不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proShortName.Text, true))
            {
                MessageBox.Show(this.Page, "产品简称不能为空！");
                return;
            }
            if (!VerifyField(FieldType.DATE, beginDate.Text, true))
            {
                MessageBox.Show(this.Page, "产品开始时间不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.DATE, endDate.Text, true))
            {
                MessageBox.Show(this.Page, "产品结束时间不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.MONEY, proPrice.Text, true))
            {
                MessageBox.Show(this.Page, "产品价格不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.MONEY, proSettlePrice.Text, true))
            {
                MessageBox.Show(this.Page, "结算金额不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.NUM, proQuota.Text, true))
            {
                MessageBox.Show(this.Page, "可购限额不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.NUM, proAgeMin.Text, true))
            {
                MessageBox.Show(this.Page, "承保最小年龄不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.NUM, proAgeMax.Text, true))
            {
                MessageBox.Show(this.Page, "承保最大年龄不能为空且必须格式正确！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proTimeDesc.Text, true))
            {
                MessageBox.Show(this.Page, "时间说明不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proIntro.Text, true))
            {
                MessageBox.Show(this.Page, "产品简介不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proDesc.Text, true))
            {
                MessageBox.Show(this.Page, "产品介绍不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proFeature.Text, true))
            {
                MessageBox.Show(this.Page, "产品特点不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, proIntro.Text, true))
            {
                MessageBox.Show(this.Page, "服务说明不能为空！");
                return;
            }
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("productTypeFid", typeList.SelectedValue));
            argList.Add(new RequestFuncParams("productInsFid", companyList.SelectedValue));
            argList.Add(new RequestFuncParams("productSupFid", supList.SelectedValue));
            argList.Add(new RequestFuncParams("productName", proName.Text));
            argList.Add(new RequestFuncParams("productShortName", proShortName.Text));
            argList.Add(new RequestFuncParams("productBeginTime", beginDate.Text));
            argList.Add(new RequestFuncParams("productEndTime", endDate.Text));
            argList.Add(new RequestFuncParams("productPrice", proPrice.Text));
            argList.Add(new RequestFuncParams("productSettlePrice", proSettlePrice.Text));
            argList.Add(new RequestFuncParams("productQuota", proQuota.Text));
            argList.Add(new RequestFuncParams("productAgeMin", proAgeMin.Text));
            argList.Add(new RequestFuncParams("productAgeMax", proAgeMax.Text));
            argList.Add(new RequestFuncParams("productIntro", proIntro.Text));
            argList.Add(new RequestFuncParams("productDesc", proDesc.Text));
            argList.Add(new RequestFuncParams("productOthers", proOthers.Text));
            argList.Add(new RequestFuncParams("productFeature", proFeature.Text));
            argList.Add(new RequestFuncParams("productServiceDesc", proServiceDesc.Text));
            argList.Add(new RequestFuncParams("productTimeDesc", proTimeDesc.Text));
            argList.Add(new RequestFuncParams("productStatus", rdoStatus.SelectedValue));
            argList.Add(new RequestFuncParams("productCode", proCode.Text)); 
            BaseService baseService = new BaseService();
            ParserXml parser;
            if (isModify)
            {
                argList.Add(new RequestFuncParams("pid", pid.ToString()));
                parser = baseService.RequestEntryService(Dict.ERequestFunc.UPDATE_PRODUCT, argList);
            }
            else
            {
                parser = baseService.RequestEntryService(Dict.ERequestFunc.ADD_PRODUCT, argList);
            }
            if (parser != null)
                GoTo(GoToType.ADDSUC);
            //string aa = proIntro.Text;
            //string bb = companyList.SelectedValue;
            //GoTo(GoToType.ADDSUC);
        }

        private void LoadData()
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("pid", pid.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_PRODUCT_BY_ID, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                typeList.SelectedValue = parser["ProductTypeFid", 0];
                companyList.SelectedValue = parser["ProductInsFid", 0];
                supList.SelectedValue = parser["ProductSupFid", 0];
                proName.Text = parser["ProductName", 0];
                proShortName.Text = parser["ProductShortName", 0];
                proName.Text = parser["ProductName", 0];
                proName.Text = parser["ProductName", 0];
                beginDate.Text = parser["ProductBeginTime", 0];
                endDate.Text = parser["ProductEndTime", 0];
                proPrice.Text = parser["ProductPrice", 0];
                proSettlePrice.Text = parser["ProductSettlePrice", 0];
                proQuota.Text = parser["ProductQuota", 0];
                proAgeMin.Text = parser["ProductAgeMin", 0];
                proAgeMax.Text = parser["ProductAgeMax", 0];
                proIntro.Text = parser["ProductIntro", 0];
                proDesc.Text = parser["ProductDesc", 0];
                proOthers.Text = parser["ProductOthers", 0];
                proFeature.Text = parser["ProductFeature", 0];
                proServiceDesc.Text = parser["ProductServiceDesc", 0];
                proTimeDesc.Text = parser["ProductTimeDesc", 0];
                rdoStatus.SelectedValue = parser["ProductStatus", 0];
                proCode.Text = parser["ProductCode", 0];
                proCode.Enabled = false;
            }

        }
    }
}
